package com.jiamao.goplay.ui.user;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.adapter.AnonymousReportPhotoAdapter;
import com.jiamao.goplay.adapter.AnonymouseReportAdapter;
import com.jiamao.goplay.adapter.onAnonymousItemClick;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.bean.AnonymousSelectBean;
import com.jiamao.goplay.databinding.AnonymouseReportLayoutBinding;
import com.jiamao.goplay.ui.user.AnonymousReportOtherFragment;
import com.jiamao.goplay.ui.user.vm.AnonymousReportVM;
import com.jiamao.goplay.view.HorizontalItemDecoration;
import com.jiamao.goplay.view.WrapHeightGridLayoutManager;
import com.yuyh.library.imgsel.ISNav;
import com.yuyh.library.imgsel.config.ISListConfig;

import java.util.ArrayList;
import java.util.List;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * 匿名举报页
 */
public class AnonymousReportFragment extends BaseMVVMFragment<AnonymouseReportLayoutBinding, AnonymousReportVM> {

    private AnonymouseReportAdapter adapter;
    private List<AnonymousSelectBean> selectReason = new ArrayList<>();

    private int index;
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.anonymouse_report_layout;
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.showPhoto.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(@Nullable Integer aBoolean) {
                index = aBoolean;
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
                showPhoto();
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        AnonymousSelectBean bean = new AnonymousSelectBean("头像、资料作假", true);
        selectReason.add(bean);
        AnonymousSelectBean bean1 = new AnonymousSelectBean("广告、营销", false);
        selectReason.add(bean1);
        AnonymousSelectBean bean2 = new AnonymousSelectBean("诈骗、托", false);
        selectReason.add(bean2);
        AnonymousSelectBean bean3 = new AnonymousSelectBean("色情低俗", false);
        selectReason.add(bean3);
        AnonymousSelectBean bean4 = new AnonymousSelectBean("恶意骚扰、不文明语言", false);
        selectReason.add(bean4);
        AnonymousSelectBean bean5 = new AnonymousSelectBean("其他", false);
        selectReason.add(bean5);

        init();

        binding.setAdapter(new AnonymousReportPhotoAdapter(getContext()));
        WrapHeightGridLayoutManager gm = new WrapHeightGridLayoutManager(getContext(), 3);
        binding.photoListView.setLayoutManager(gm);
        binding.photoListView.addItemDecoration(new HorizontalItemDecoration(8,getContext()));
        gm.setAutoMeasureEnabled(true);
    }

    @Override
    public AnonymousReportVM initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(AnonymousReportVM.class);
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    private void init() {

        viewModel.uid = getArguments().getString("uid");
        TextView textViewTitle = binding.getRoot().findViewById(R.id.tvTitleReport);
        textViewTitle.setText("匿名举报");
        binding.getRoot().findViewById(R.id.ll_back_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });

        //提交举报
        binding.getRoot().findViewById(R.id.ll_submit_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //ToastUtils.showShort("提交举报");

                viewModel.sendPost();
            }
        });

        adapter = new AnonymouseReportAdapter(getContext(), selectReason);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        binding.rcChooseReason.setLayoutManager(layoutManager);
        binding.rcChooseReason.setAdapter(adapter);

        adapter.setOnItemClickListener(new onAnonymousItemClick() {
            @Override
            public void onItemCLick(int position, String value) {
                viewModel.type = position;
                if (value.equals("其他")) {

                    Bundle bd =  new Bundle();
                    bd.putString("uid",viewModel.uid);
                    startContainerActivity(AnonymousReportOtherFragment.class.getCanonicalName(),bd);

                   // startContainerActivity(AnonymousReportOtherFragment.class.getCanonicalName());
                }
            }
        });


    }

    public void showPhoto(){
        // 自由配置选项
        ISListConfig config = new ISListConfig.Builder()
                // 是否多选, 默认true
                .multiSelect(false)
                // 是否记住上次选中记录, 仅当multiSelect为true的时候配置，默认为true
                .rememberSelected(false)
                // “确定”按钮背景色
                .btnBgColor(Color.GRAY)
                // “确定”按钮文字颜色
                .btnTextColor(Color.BLUE)
                // 使用沉浸式状态栏
                .statusBarColor(Color.WHITE)
                // 返回图标ResId
                .backResId(R.mipmap.ic_black_back)
                // 标题
                .title("图片")
                // 标题文字颜色
                .titleColor(Color.BLACK)
                // TitleBar背景色
                .titleBgColor(Color.WHITE)
                // 裁剪大小。needCrop为true的时候配置
                .cropSize(1, 1, 200, 200)
                .needCrop(true)
                // 第一个是否显示相机，默认true
                .needCamera(false)
                // 最大选择图片数量，默认9
                .maxNum(1)
                .build();

// 跳转到图片选择器
        ISNav.getInstance().toListActivity(this, config, 1);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 图片选择结果回调
        if (requestCode ==  1  && data != null) {
            List<String> pathList = data.getStringArrayListExtra("result");

            viewModel.uploadImage(pathList.get(0),index);

        }
    }
}
